// pages/shangluoyuehuiquncheng/shangluoyuehuiquncheng.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: Number, // 起始页码
    videlist: [],
  },

  
  clear() {
    console.log('最大页码', this.data.totalPage);
    this.setData({
      page: 1,
      videlist: []
    })
    setTimeout(() => {
      this.getvidelist()
    }, 500);
  },
  onSearch(event) {
    const n = event.detail
    this.getvidelist(n)
  },
  onchange(event) {
    const value = event.detail
    if (value == '') {
      console.log('8585', this.data.page);
      this.setData({
        page: 1, // 起始页码
        videlist: [],
      })
      // setTimeout(() => {
      //   this.getvidelist()
      // }, 500);

    }
  },



  
  getvidelist(n = '') {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })



    wx.request({
      url: app.globalData.interface+'/shangLuoWork/API_2029_shanxiBusinessList ',
      method: 'POST',
      data: {
        KeyWord: n,
        currentPage: '', //当前页   非必填
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res', res);
        console.log('res', res.data.result);
        wx.hideLoading()
        if (res.data.result == 'success' && res.data.data.length != 0) {
          console.log('翻页', res.data.data[0].page.totalPage);

          const copypage = JSON.stringify(res.data.data[0].page.totalPage)
          const totalPage = JSON.parse(copypage)
          console.log('totalPage', totalPage);

          console.log(res.data.data, );
          this.setData({
            videlist: res.data.data,
            totalPage: totalPage
          })

          console.log('最大页码数', this.data.totalPage);
        } else {
          wx.showModal({
            title: "暂无该条数据",
            content: '无数据',
            showCancel: false,
          });
        }
      },
      fail: (res => {
        wx.hideLoading()
      })
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      page: 1, // 起始页码
    })

    setTimeout(() => {
      this.getvidelist()
    }, 500);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let totalP = this.data.totalPage //最大页码数
    const that = this
    let page = that.data.page + 1 // 当前页码+1，获取下一页的数据
    console.log('page', page);

    if (page <= totalP && totalP > 1) {
      that.setData({
        page: page,
      })
      wx.request({
        url: app.globalData.interface+'/shangLuoWork/API_2029_shanxiBusinessList',
        method: 'POST',
        data: {

          KeyWord: '',
          currentPage: '', //当前页   非必填
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);

          if (res.data.result == 'success') {


            that.setData({
              videlist: [...that.data.videlist, ...res.data.data], //es6中的在旧数据中加其他数据,
            })

            // that.findXy()

            wx.hideLoading()
            console.log('最大页码数', this.data.totalPage);
            wx.showToast({
              title: '数据请求成功',
              icon: 'success',
              duration: 1000,
            })

          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })
      console.log("56565656", this.data.page);

    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'error',
        duration: 1500,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})